﻿using GetSlaughterItem.API.Application.Commands.Week01.Addrs;
using MediatR;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Domains.Manages.Addr;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week01.Manageres
{
    public class ClientsSuretenHandler : IRequestHandler<ClientsSuretenCommend, Result>
    {
        
        private readonly IBaseRepositoryGet<Provibces> Provibces;
        private readonly IBaseRepositoryGet<Citys> Citys;
        private readonly IBaseRepositoryGet<Areas> Areas;
        private readonly IBaseRepositoryGet<Clients> Clients;

        public ClientsSuretenHandler(IBaseRepositoryGet<Provibces> Provibces, IBaseRepositoryGet<Citys> Citys,
            IBaseRepositoryGet<Areas> Areas, IBaseRepositoryGet<Clients> Clients)
        {
            this.Provibces = Provibces;
            this.Citys = Citys;
            this.Areas = Areas;
            this.Clients = Clients;
        }

        public async Task<Result> Handle(ClientsSuretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in Clients.GetAll()
                            join b in Provibces.GetAll() on a.ProvibceId equals b.ProvibceId
                            join c in Citys.GetAll() on a.CityId equals c.CityId
                            join d in Areas.GetAll() on a.AreaId equals d.AreaId
                            where a.IsDelete == false
                            select new 
                            {
                                a.CustomerId,
                                a.CustomerNickname,
                                a.CustomerIDcar,
                                a.CustomerCooperateTime,
                                a.SectionId,
                                a.CustomerState,
                                a.CustomerNotes,
                                b.ProvibceId,
                                b.ProvibceName,
                                c.CityId,
                                c.CitysName,
                                d.AreaId,
                                d.AreaName,

                            }).ToList();

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
